package me.send.produce.data.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import me.send.business.base.Pager;
import me.send.business.base.SendVariable;
import me.send.business.base.jpush.JPush;
import me.send.data.service.bean.ReturnJsonBean;
import me.send.data.service.cons.MessageCons;
import me.send.data.service.pojo.Broker;
import me.send.data.service.pojo.Manager;
import me.send.data.service.pojo.Message;
import me.send.data.service.service.BrokerService;
import me.send.data.service.service.TreeService;
import me.send.produce.data.controller.common.CommonController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jpush.api.JPushClient;

@Controller
@RequestMapping(value = "broker")
public class BrokerController extends CommonController {

	private static final String AUTHEN_PASS_PUSH_STR="您提交的经纪人审核申请已通过，快去抢单吧";
	private static final String AUTHEN_REFUSE_PUSH_STR="您提交的经纪人审核申请被拒绝，拒绝原因：";
	
	@Autowired
	private BrokerService brokerService;

	@Autowired
	private TreeService treeService;

	/**
	 * 批量删除popuser表中的经纪人信息
	 * @param brokerids 经纪人id列表，以,分开，如"3,4,5"
	 * @return 状态码，提示删除操作成功还是失败
	 */
	@RequestMapping(value = "batch/delete")
	@ResponseBody
	public Map<String, Object> batch_delete(String brokerids) {
		return brokerService.batch_delete(brokerids);
	}

	// 查询经纪人列表
	@RequestMapping(value = "broker/list")
	public ModelAndView broker_list(HttpServletRequest request) {
		Manager manager =(Manager)request.getSession().getAttribute("currentUser");
		request.setAttribute("manager", manager);
		ModelAndView modelAndView = new ModelAndView(
				"stage/broker/broker_list");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "broker/broker/list"));
		modelAndView.addObject("module", "经纪人列表");
		return modelAndView;
	}

	@RequestMapping(value = "page")
	@ResponseBody
	public Map<String, Object> page(Pager page, Broker broker,String cityName) {
		return brokerService.page(page, broker,cityName);
	}

	@RequestMapping(value = "broker/login")
	@ResponseBody
	public Map<String, Object> broker_login(HttpServletRequest request,
			String username, String password) {
		Map<String, Object> map = brokerService
				.broker_login(username, password);
		// 获取下面的用户信息 2015年6月19日11:38:14
		HttpSession session = request.getSession();
		// 将用户的信息放在session下面 2015年6月19日11:38:44
		@SuppressWarnings("unchecked")
		Map<String, Object> result = (Map<String, Object>) map
				.get(SendVariable.RESULTMAP);
		session.setAttribute("user", result);
		session.setAttribute(SendVariable.VARIABLEMAP,
				SendVariable.sendvariable_map);
		// 加载一个路径
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path + "/";
		session.setAttribute("ctx", basePath);
		/** 使用加载树 的数据 2015年6月19日15:27:56 **/
		session.setAttribute("LoadTree",
				treeService.main_tree(0, request, null));
		return map;
	}
	
	/**
	 * 跳转到提交申请的经纪人列表
	 * @param request
	 * @param model
	 * @return
	 * @author maxmin
	 * @date 2015年9月15日下午5:29:31
	 */
	@RequestMapping(value = "/unauthen/list")
	public ModelAndView toUnAuthenedBrokers(HttpServletRequest request){
		Manager manager =(Manager)request.getSession().getAttribute("currentUser");
		request.setAttribute("manager", manager);
		ModelAndView modelAndView = new ModelAndView("stage/broker/broker_unauthen_list");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "broker/unauthen/list"));
		return modelAndView;
	}
	

	/**
	 * 获取提交申请的列表
	 * @param page
	 * @param broker
	 * @return
	 * @author maxmin
	 * @date 2015年9月22日上午9:11:08
	 */
	@RequestMapping(value = "/unauthen")
	@ResponseBody
	public Map<String, Object> getUnAuthenedBrokers(Pager page, Broker broker,String cityName){
		return brokerService.unAuthenedList(page, broker,cityName);
	}
	
	/**
	 * 跳转到详情页面
	 * @param request
	 * @param popid
	 * @return
	 * @author maxmin
	 * @date 2015年9月21日下午4:33:50
	 */
	@RequestMapping(value = "/detail")
	public ModelAndView getBrokerDetail(HttpServletRequest request,int popid){
		ModelAndView modelAndView = new ModelAndView("stage/broker/broker_detail");
		Broker broker = brokerService.getBrokerDetail(popid);
		modelAndView.addObject("broker", broker);
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "broker/unauthen/list"));
		return modelAndView;
	}
	
	@RequestMapping(value="/authen/pass")
	@ResponseBody
	public ReturnJsonBean authenPass(Integer brokerid){
		ReturnJsonBean rb = new ReturnJsonBean();
		if (brokerid!=null) {
			int result = brokerService.authenPass(brokerid);
			if (result>0) {
				Broker broker = brokerService.getBrokerDetail(brokerid);
				rb.setCode(ReturnJsonBean.SUCCESS);
				rb.setMessage(ReturnJsonBean.SUCCESS_STR);
				JPushClient client = JPush.getBrokerJPushClient();
				JPush.pushWithAlias(client, AUTHEN_PASS_PUSH_STR, broker.getIsvAccountId());
				//记录消息
				Message message = new Message(AUTHEN_PASS_PUSH_STR,brokerid,MessageCons.USER_TYPE_BROKER,MessageCons.MESSAGE_TYPE_ORDER);
				saveMessage(message);
			}
		}
		return rb;
	}
	
	@RequestMapping(value="/authen/refuse")
	@ResponseBody
	public ReturnJsonBean authenRefuse(Integer brokerid,String reason){
		ReturnJsonBean rb = new ReturnJsonBean();
		if (brokerid!=null) {
			int result = brokerService.authenRefuse(brokerid);
			if (result>0) {
				Broker broker = brokerService.getBrokerDetail(brokerid);
				rb.setCode(ReturnJsonBean.SUCCESS);
				rb.setMessage(ReturnJsonBean.SUCCESS_STR);
				JPushClient client = JPush.getBrokerJPushClient();
				JPush.pushWithAlias(client, AUTHEN_REFUSE_PUSH_STR+reason, broker.getIsvAccountId());
				//记录消息
				Message message = new Message(AUTHEN_REFUSE_PUSH_STR+reason,brokerid,MessageCons.USER_TYPE_BROKER,MessageCons.MESSAGE_TYPE_ORDER);
				saveMessage(message);
			}
		}
		return rb;
	}
	
}
